import { doInfo, doVersion, doMendian, doMendianInfo } from "@/api/user.js";
const state = {
  timestamp: 60,
  pid: "",
  isShow: false,
  location: "杭州",
  version: "0.0.1",
  sex: uni.getStorageSync("sex"),
  openId: {},
  token: "",
  userinfo: {},
  consumerHotline: "",
  postions: {},
  commonLoading: true,
  isFirstOver: false,
  storeInfo: {
    id: "",
  },
  mylocation: {},
  topfontsize: 33,
  chancefontsize: 38,
  weight: "400",
};

const mutations = {
  setmylocation(state, info) {
    state.mylocation = info;
  },
  settopfontsize(state, data) {
    state.topfontsize = data;
  },
  setweight(state, data) {
    state.weight = data;
  },
  setchancefontsize(state, data) {
    state.chancefontsize = data;
  },
  SETTIME(state) {
    state.timestamp = state.timestamp - 1;
  },
  SETINITTIME(state, time) {
    state.timestamp = time;
  },
  SETLOCATION(state, e) {
    state.location = e;
  },
  closeFirst(state) {
    state.isFirstOver = true;
  },
  SETSTOREINFO(state, info) {
    state.storeInfo = info;
  },
  CLOSELOADING(state) {
    state.commonLoading = false;
  },
  setAddlog(state, postions) {
    state.postions = postions;
  },
  setPid(state, pid) {
    state.pid = pid;
  },
  setSex(state, sex) {
    state.sex = sex;
  },
  setOpenId(state, info) {
    state.openId = info;
  },
  setToken(state, token) {
    state.token = token;
  },
  SETUSERINFO(state, info) {
    state.userinfo = info;
  },
  SETSHOW(state, show) {
    state.isShow = show;
  },
  setPhone(state, phone) {
    state.consumerHotline = phone;
  },
};

const actions = {
  setTime({ commit, state, dispatch }, count = 0) {
    if (count > 0) {
      commit("SETINITTIME", count);
    }
    commit("SETTIME");
    if (state.timestamp > 0) {
      setTimeout(() => {
        dispatch("setTime");
      }, 1000);
    }
  },
  closeLoading({ commit }) {
    commit("CLOSELOADING");
  },
  async setStoreId({ commit }, id) {
    commit("SETSTOREINFO", {
      id,
    });
    return new Promise((resolve, reject) => {
      doMendianInfo()
        .then(({ code, data }) => {
          if (code === 200) {
            commit("SETSTOREINFO", data);
            resolve(data);
          } else {
            reject(code);
          }
        })
        .catch(err => {
          reject(res);
        });
    });
  },
  async getAddress({ commit }, res) {
    return new Promise(async (resolve, reject) => {
      try {
        const { data } = await doMendian(res);
        var startTime = new Date();
        var startDate = new Date(startTime);
        startDate.setDate(startDate.getDate() + 30);
        uni.setStorageSync(
          "storeId",
          JSON.stringify({
            id: data.id,
            time: startDate.getTime1(),
          })
        );
        commit("SETSTOREINFO", data);
        commit("setAddlog", res);
        resolve(res);
      } catch (e) {
        reject(e);
        if (!e) {
          console.log("获取失败");
        }
      }
    });
  },
  setPhone({ commit }, phone) {
    commit("setPhone", phone);
  },
  setPid({ commit }, id) {
    commit("setPid", id);
  },
  setOpenId({ commit }, info) {
    info.sessionKey = info.session_key;
    commit("setOpenId", info);
  },
  async getUserInfo({ commit, state }) {
    return new Promise((rel, rej) => {
      doInfo()
        .then(res => {
          if (res.code !== 200) {
            dispatch("signOut");
            rej(res.msg);
          } else {
            commit("SETUSERINFO", res.data);
            rel(res.data);
          }
        })
        .catch(err => {
          console.log(err);
          rej(err);
        });
    });
  },
  overFirst({ commit }) {
    commit("closeFirst");
  },
  async setToken({ commit, dispatch }, token) {
    return new Promise((rel, rej) => {
      commit("setToken", token);
      uni.setStorageSync("token", token);
      doInfo()
        .then(res => {
          if (res.code !== 200) {
            commit("closeFirst");
            dispatch("signOut");
            rej(res.msg);
          } else {
            commit("SETUSERINFO", res.data);
            // if (!state.storeInfo?.id) {
            // 	commit('SETSTOREINFO', res.data.store_info[0])
            // }
            commit("closeFirst");
            rel(res.data);
          }
        })
        .catch(err => {
          console.log(err);
          rej(err);
        });
    });
  },
  setTest({ commit }, text) {
    commit("setTest", text);
  },
  setSex({ commit }, sex) {
    uni.setStorageSync("sex", sex);
    commit("setSex", sex);
  },
  getVersion({ commit }) {
    doVersion().then(({ data }) => {
      commit("SETSHOW", !data);
    });
  },
  signOut({ commit }) {
    uni.removeStorageSync("token");
    commit("setToken", null);
    commit("SETUSERINFO", {});
  },
};

const getters = {
  test: state => state.test,
  timestamp: state => state.timestamp,
  pagePid: state => state.pid,
  commonLoading: state => state.commonLoading,
  cellPhone: state => state.userinfo?.phone || 0,
  consumerHotline: state => state.consumerHotline,
  pid: state => state.pid,
  isShow: state => state.isShow,
  version: state => state.version,
  openId: state => state.openId,
  token: state => state.token,
  sex: state => state.sex,
  userinfo: state => state.userinfo,
  integral: state => state.userinfo?.integral || 0,
  storeId: state => state.storeInfo?.id || "",
  storeInfo: state => state.storeInfo || {},
  storeName: state => state.storeInfo?.store_name || "杭州绿牌汽车",
  firstOver: state => state.isFirstOver,
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
